package com.zhenyi.xiaohuoche.index;

import java.math.BigDecimal;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xhc.common.model.XhcOrder;
import com.xhc.common.model.XhcPresent;
import com.xhc.common.model.XhcUserPresent;
import com.xhc.common.model.XhcWechat;
import com.zhenyi.xhc.core.XHCController;

public class PresentController extends XHCController {

	@Override
	public void list() {
		SqlPara sqlPara = xhcDb.getSqlPara("XHCProducts");
		List<XhcPresent> items = XhcPresent.dao.find(sqlPara);
		renderResult(items);
	}

	@Override
	public void info() {
		Long id = getParaToLong("id", null);
		if (id == null) {
			renderFailure("id is null");
			return;
		}
		XhcPresent Present = XhcPresent.dao.findById(id);
		renderResult(Present);
	}

	/**
	 * 礼物退款
	 */
	@Before(Tx.class)
	public void refund() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure();
			return;
		}
		orderUtils.refundPresent(this, id);
	}

	public void pay() {
		Long presentId = getParaToLong("presentId", 1L);
		if (presentId == null) {
			renderFailure();
			return;
		}
		Integer count = getParaToInt("count");
		if (count == null) {
			renderMsg("至少购买一份哦");
			return;
		}
		String userName = getPara("userName", "userName");
		String postalCode = getPara("postalCode", "postalCode");
		String provinceName = getPara("provinceName", "provinceName");
		String cityName = getPara("cityName", "cityName");
		String countyName = getPara("countyName", "countyName");
		String detailInfo = getPara("detailInfo", "detailInfo");
		String nationalCode = getPara("nationalCode", "nationalCode");
		String telNumber = getPara("telNumber", "telNumber");
		XhcPresent present = XhcPresent.dao.findById(presentId);
		BigDecimal total = present.getPrice().multiply(new BigDecimal(count));
		System.out.println("total: " + total.toString());
		XhcUserPresent userPresent = new XhcUserPresent();
		userPresent.setTotal(total);
		total = total.multiply(new BigDecimal(100));
		XhcOrder order = genOrder(total.intValue() + "", JsonKit.toJson(present), "present");
		XhcWechat wechat = getWechat();
		Long orderId = order.getId();
		userPresent.setCount(count);
		userPresent.setUserName(userName);
		userPresent.setPostalCode(postalCode);
		userPresent.setProvinceName(provinceName);
		userPresent.setCityName(cityName);
		userPresent.setCountyName(countyName);
		userPresent.setDetailInfo(detailInfo);
		userPresent.setNationalCode(nationalCode);
		userPresent.setTelNumber(telNumber);
		userPresent.setOrderId(orderId);
		userPresent.setWechatId(wechat.getId());
		userPresent.setStatus(0);
		userPresent.setAddTime();
		userPresent.setPresentId(presentId);
		userPresent.setPaid(total);
		userPresent.setRaw(present.toJson());
		userPresent.setTitle(present.getTitle());
		userPresent.setLogo(present.getCover());
		String address = countyName + provinceName + cityName + detailInfo;
		userPresent.setAddress(address);
		boolean flag = userPresent.save();
		if (!flag) {
			renderFailure();
			return;
		}
		renderPay(order.getOutTradeNo(), present.getTitle(), total.intValue() + "", wechat.getOpenId(), "present");
	}

	public void orders() {
		XhcWechat wechat = getWechat();
		SqlPara sqlPara = xhcDb.getSqlPara("XHCPresentsOrder", wechat.getId());
		List<Record> items = xhcDb.find(sqlPara);
		renderResult(items);
	}

	public void orderInfo() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure();
			return;
		}
		XhcUserPresent userPresent = XhcUserPresent.dao.findById(id);
		renderResult(userPresent);
	}

}
